home *** CD-ROM | disk | FTP | other *** search
/ Delphi Magazine Collection 2001 / Delphi Magazine Collection 20001 (2001).iso / DISKS / Issue25 / compress / COMPRESS.ZIP / COMPMAIN.PAS < prev    next >
Encoding:
Pascal/Delphi Source File  |  1997-07-09  |  27.4 KB  |  803 lines

  1. (*
  2.   CompDemo for TCompress Components V3.5
  3.  
  4.   Note: This demo is currently saved in Delphi 2.0/3.0 format -- see the notes below
  5.   for minor adaptations required to compile it with Delphi 1.0.
  6.  
  7.   You are free to amend, adjust, improve, update, borrow, alter and play
  8.   with this demonstration program at will.
  9.  
  10.   However, if you redistribute the unregistered TCompress components, please be
  11.   sure to include ALL the files that came with it (incl. Compress.hlp, Readme.txt
  12.   and the ORIGINAL COMPDEMO source).  Thanks.
  13.  
  14.   Hint: To find the code which makes use of the TCompress components, search
  15.   for Compress1, CDBImage1 and CDBMemo1 references...  At some point, you may
  16.   also want to modify this demo to play with the Key, TargetPath and
  17.   MakeDirectories properties of the TCompress component (all new in V2.5), or
  18.   to experiment with the CompressStreamToArchive method (new in V3.0) of which
  19.   a sample is given in SaveDirectToArchive.
  20.  
  21.   USING THIS DEMO with Delphi V1.0:
  22.   1. Copy COMPDEMO.DPR, COMPMAIN.PAS and COMPMAIN.DFM to a new directory
  23.   2. Load Delphi 1.0, install Compress/Compctrl and load the new project
  24.   3. Ignore errors about duplicated components and Blobtype properties (not in Delphi 1.0)
  25.   4. In the CheckFile event handler, change the filepath type from string
  26.      (Delphi 2.0+) to OpenString (Delphi 1.0). Don't forget to do this in
  27.     the method declaration as well as its implementation.
  28.   5. Compile and go.  Be aware that you may need to add special filename
  29.      handling in Checkfile to deal with any archives already compressed with
  30.      looong (Win95+) filenames in them. Basically, just truncate to
  31.      a suitable 8.3 format name.
  32.  
  33.   Enjoy.
  34. *)
  35.  
  36. unit Compmain;
  37.  
  38. interface
  39.  
  40. uses
  41.   SysUtils, WinTypes, WinProcs, Messages, Classes, Graphics, Controls,
  42.   Forms, Dialogs, Compress, StdCtrls, DB, DBTables, DBCtrls
  43.   ,CompCtrl, ExtCtrls, Buttons, FileCtrl, Mask;
  44.  
  45.  
  46. type
  47.   TForm1 = class(TForm)
  48.     Table1: TTable;
  49.     DBNavigator1: TDBNavigator;
  50.     DataSource1: TDataSource;
  51.     Compress1: TCompress;
  52.     Table1SpeciesNo: TFloatField;
  53.     Table1Category: TStringField;
  54.     Table1Common_Name: TStringField;
  55.     Table1SpeciesName: TStringField;
  56.     Table1Lengthcm: TFloatField;
  57.     Table1Length_In: TFloatField;
  58.     CMethod: TRadioGroup;
  59.     Memo2: TMemo;
  60.     Shape1: TShape;
  61.     GroupBox1: TGroupBox;
  62.     FL: TFileListBox;
  63.     DL: TDirectoryListBox;
  64.     DCB: TDriveComboBox;
  65.     ArchiveGroup: TGroupBox;
  66.     ArchiveLabel: TLabel;
  67.     archivefile: TEdit;
  68.     Label2: TLabel;
  69.     ListBox1: TListBox;
  70.     Fishname: TDBEdit;
  71.     Memo4: TMemo;
  72.     Memo3: TMemo;
  73.     Memo5: TMemo;
  74.     Memo6: TMemo;
  75.     DBText1: TDBText;
  76.     Memo1: TMemo;
  77.     Button1: TButton;
  78.     Panel1: TPanel;
  79.     Bevel1: TBevel;
  80.     Time: TLabel;
  81.     Percentage: TLabel;
  82.     TimeLabel: TLabel;
  83.     Label7: TLabel;
  84.     Trashcan: TImage;
  85.     Image1: TImage;
  86.     Button2: TButton;
  87.     CDBImage1: TCDBImage;
  88.     CDBMemo1: TCDBMemo;
  89.     Button3: TButton;
  90.     procedure CompressOneFile(var fname: String);
  91.     procedure ResetFileInfo;
  92.     function GetDir: string;
  93.     function GetDummyFilename(generatefrom: string; ext: string): string;
  94.     procedure handleDropField(Source: TObject; archivetoo: Boolean);
  95.     procedure SaveDirectToArchive(Source: TField; filename: string);
  96.     procedure CompressFiles;
  97.     function getCompressionMethod: TCompressionMethod;
  98.     procedure showInfo;
  99.     procedure FormCreate(Sender: TObject);
  100.     procedure showfiles;
  101.     procedure ExpandDelete(Operation: TCProcessMode; All: Boolean);
  102.     procedure archivefileChange(Sender: TObject);
  103.     procedure CMethodClick(Sender: TObject);
  104.     procedure DLDragOver(Sender, Source: TObject; X, Y: Integer;
  105.       State: TDragState; var Accept: Boolean);
  106.     procedure CDBImage1DragOver(Sender, Source: TObject; X, Y: Integer;
  107.       State: TDragState; var Accept: Boolean);
  108.     procedure CDBImage1DragDrop(Sender, Source: TObject; X, Y: Integer);
  109.     procedure CDBMemo1DragOver(Sender, Source: TObject; X, Y: Integer;
  110.       State: TDragState; var Accept: Boolean);
  111.     procedure CDBMemo1DragDrop(Sender, Source: TObject; X, Y: Integer);
  112.     procedure CDBImage1MouseDown(Sender: TObject; Button: TMouseButton;
  113.       Shift: TShiftState; X, Y: Integer);
  114.     procedure CDBMemo1MouseDown(Sender: TObject; Button: TMouseButton;
  115.       Shift: TShiftState; X, Y: Integer);
  116.     procedure archivefileDragOver(Sender, Source: TObject; X, Y: Integer;
  117.       State: TDragState; var Accept: Boolean);
  118.     procedure archivefileDragDrop(Sender, Source: TObject; X, Y: Integer);
  119.     procedure DLDragDrop(Sender, Source: TObject; X, Y: Integer);
  120.     procedure TrashcanDragDrop(Sender, Source: TObject; X, Y: Integer);
  121.     procedure FormDestroy(Sender: TObject);
  122.     procedure ListBox1Click(Sender: TObject);
  123.     procedure Table1AfterPost(DataSet: TDataset);
  124.     procedure Button1Click(Sender: TObject);
  125.     procedure FLClick(Sender: TObject);
  126.     procedure Compress1CheckFile(var filepath: String;
  127.       mode: TCProcessMode);
  128.     procedure Panel1Click(Sender: TObject);
  129.     procedure FormClick(Sender: TObject);
  130.     procedure GroupBox1Click(Sender: TObject);
  131.     procedure TrashcanDragOver(Sender, Source: TObject; X, Y: Integer;
  132.       State: TDragState; var Accept: Boolean);
  133.     procedure Button2Click(Sender: TObject);
  134.     procedure Compress1ShowProgress(var PercentageDone: Longint);
  135.     procedure Button3Click(Sender: TObject);
  136.     procedure disabledragMode;
  137.     procedure enabledragMode;
  138.   private
  139.     { Private declarations }
  140.   public
  141.     { Public declarations }
  142.   end;
  143.  
  144. var
  145.   Form1: TForm1;
  146.  
  147. implementation
  148.  
  149. {$R *.DFM}
  150.  
  151. var FileList: TStringList; { holds information about our archive files }
  152.     saveCompressionMethod: Integer; { see ListBox1.click }
  153.  
  154. const ShowFileInfoColor :Tcolor = clGray; { see Listbox1.click }
  155.  
  156. { Example of accessing the TCompress performance properties }
  157. procedure Tform1.showinfo;
  158. begin
  159.    ResetFileInfo;
  160.    Time.caption:=Format('%-5.1fsecs',[Compress1.CompressionTime/1000.0]{[f]});
  161.    Percentage.caption:=IntToStr(Compress1.CompressedPercentage)+'%';
  162. end;
  163.  
  164. { Example of a progress event (new in TCompress 2.0) }
  165. procedure TForm1.Compress1ShowProgress(var PercentageDone: Longint);
  166. begin
  167.    Percentage.caption:=IntToStr(PercentageDone)+'%';
  168.    Application.ProcessMessages;
  169.   { you may have *other* uses for this every-8K-read event...  In fact, in V2.5
  170.     if you set PercentageDone to -1, it will cause compression to end at the
  171.     point reached. If so, delete from the archive the compressed file
  172.     which was created before the abort  }
  173. end;
  174.  
  175. { Example of getting a list of files in a multi-file archive }
  176. procedure TForm1.showfiles;
  177. begin
  178.   listbox1.clear;
  179.   Compress1.FreeFileList(FileList); { clear list and free any file information objects in it }
  180.   if not FileExists(archivefile.Text) then exit;
  181.   Compress1.ScanCompressedFile(ArchiveFile.Text,Filelist);
  182.   ListBox1.Items.addStrings(FileList); { and File info objects are
  183.                             there too -- see ListBox1Click and FormDestroy }
  184. end;
  185.  
  186. { Example of expanding/deleting one or more files from a multi-file archive }
  187. procedure TForm1.ExpandDelete(Operation: TCProcessMode; All: Boolean);
  188. var s: Tstringlist;
  189.   count: Integer;
  190. begin
  191.   if (All and (Listbox1.Items.count > 0)) or (Listbox1.selcount>0) then { something is... }
  192.   begin
  193.      s:=Tstringlist.create;
  194.      try
  195.         if All then
  196.            s.addStrings(ListBox1.Items)
  197.         else
  198.            for count :=0 to Listbox1.ITems.count-1 do
  199.             if Listbox1.selected[count] then
  200.               s.add(Listbox1.items[count]);
  201.         if Operation=cmExpand then { expand }
  202.           compress1.expandfiles(ArchiveFile.Text,s)
  203.         else
  204.           compress1.deletefiles(ArchiveFile.Text,s);
  205.         showinfo;
  206.         showfiles; { also clears selections... }
  207.      finally
  208.         s.free;
  209.         Screen.Cursor := crDefault;
  210.      end;
  211.   end;
  212. end;
  213.  
  214. { Example of compressing a SINGLE file into an archive }
  215. procedure TForm1.CompressOneFile(var fname: String);
  216. begin
  217.   disableDragMode;
  218.   try
  219.     Compress1.CompressFile(ArchiveFile.Text,fname,getCompressionMethod);
  220.     showInfo;
  221.     showfiles;
  222.   finally
  223.     Screen.Cursor := crDefault;
  224.     enableDragMode;
  225.   end;
  226.   SysUtils.DeleteFile(fname); { because for this example we're creating TEMP files only... }
  227. end;
  228.  
  229. { Example of compressing MULTIPLE files into an archive }
  230. procedure TForm1.CompressFiles;
  231. var s: Tstringlist;
  232.     Count: Integer;
  233. begin
  234.   if FL.selcount>0 then { something is... }
  235.   begin
  236.     s:=TStringlist.Create;
  237.     try
  238.       disableDragMode;
  239.       for count :=0 to FL.Items.count-1 do
  240.         if FL.selected[count] then
  241.           s.add(FL.items[count]);
  242.       Compress1.CompressFiles(ArchiveFile.Text,s,getCompressionMethod);
  243.       showInfo;
  244.       showfiles;
  245.     finally;
  246.        s.free;
  247.        Screen.Cursor := crDefault;
  248.        enableDragMode;
  249.     end;
  250.   end;
  251. end;
  252.  
  253. { Examples of setting/loading/shifting image blobs }
  254. procedure TForm1.CDBImage1DragDrop(Sender, Source: TObject; X, Y: Integer);
  255. var filepath: String;
  256.     isCenterImage: Boolean;
  257. begin
  258.    if Source=Sender then exit; { nowt to do }
  259.    isCenterImage := (Sender=Image1) or (Sender=Memo1);
  260.    if (Sender is TCDBImage) and (not Table1.active) then
  261.    begin
  262.      showmessage('Can''t do this unless table has been opened...');
  263.      exit;
  264.    end;
  265.  
  266.   Screen.Cursor:= crHourGlass;
  267.   if (Source = Image1) and (Sender is TCDBImage) then
  268.   begin
  269.      Table1.edit;
  270.      CDBImage1.picture.bitmap.Assign(Image1.Picture.bitmap)
  271.   end
  272.   else if (Source is TCDBImage) and isCenterImage then
  273.      Image1.picture.bitmap.Assign(CDBImage1.Picture.Bitmap)
  274.   else
  275.   begin   { Have we got an image? }
  276.      filepath := '';
  277.      if (Source is TListBox) and (Listbox1.selcount = 1) then
  278.       filepath:=ListBox1.Items[Listbox1.ItemIndex] { archive list }
  279.      else if (Source is TFileListBox) and (FL.selcount=1) then
  280.         filepath:=FL.Items[FL.ItemIndex]; { file list }
  281.      if LowerCase(ExtractFileExt(filepath))<>'.bmp' then
  282.      begin
  283.         MessageBeep(1);
  284.         showmessage('Must be a .BMP file...')
  285.      end else begin                             { ok, here we go... }
  286.         if Source is TListBox then { must first extract file... }
  287.         begin { Note: see ARC2BLOB.PAS and ARC2MEM.PAS for three FASTER ways
  288.                         of going about this (no expanded file needed) }
  289.           try
  290.             Compress1.ExpandFile(filepath,ArchiveFile.Text);
  291.           finally
  292.             Screen.cursor := crDefault; { as our OnCheckFile sets it on }
  293.           end;
  294.           if filepath='' then exit; { was skipped on confirmation }
  295.         end;
  296.         Screen.Cursor:= crHourGlass;
  297.         if isCenterImage then
  298.            Image1.Picture.Bitmap.LoadFromfile(filepath)
  299.         else begin
  300.            Table1.edit;
  301.            CDBImage1.Picture.Bitmap.LoadFromFile(filepath);
  302.         end
  303.      end; { else }
  304.   end;
  305.   if not Image1.Picture.Bitmap.Empty then
  306.   begin
  307.    Memo1.visible := False; { got a piccy showing... }
  308.    image1.visible := True;
  309.   end;
  310.   Screen.Cursor:= crDefault;
  311. end;
  312.  
  313. { Examples of setting/loading/shifting CDBMemo blobs }
  314. procedure TForm1.CDBMemo1DragDrop(Sender, Source: TObject; X, Y: Integer);
  315. var filepath: String;
  316. begin
  317.   if Source=Sender then exit; { nowt to do }
  318.   filepath := ''; { in case fails }
  319.   if (Source is TListBox) and (Listbox1.selcount = 1) then
  320.    filepath:=ListBox1.Items[Listbox1.ItemIndex] { archive list }
  321.   else if (Source is TFileListBox) and (FL.selcount=1) then
  322.      filepath:=FL.Items[FL.ItemIndex]; { file list }
  323.   if LowerCase(ExtractFileExt(filepath))<>'.txt' then
  324.   begin
  325.     MessageBeep(1);
  326.     showmessage('Must be a .TXT file...')
  327.   end else begin                             { ok, here we go... }
  328.     if Source is TListBox then { must first extract file... }
  329.     begin { Note: see ARC2BLOB.PAS and ARC2MEM.PAS for three FASTER ways
  330.                        of going about this (no expanded file needed) }
  331.       try
  332.         Compress1.ExpandFile(filepath,ArchiveFile.Text);
  333.       finally
  334.         Screen.cursor := crDefault; { as our OnCheckFile sets it on }
  335.       end;
  336.       if filepath='' then exit; { was skipped on confirmation }
  337.     end;
  338.     Screen.Cursor:= crHourGlass;
  339.     Table1.edit;
  340.     CDBMemo1.Lines.LoadfromFile(filepath)
  341.   end;
  342.   Screen.Cursor:= crDefault;
  343. end;
  344.  
  345. procedure TForm1.CDBMemo1DragOver(Sender, Source: TObject; X, Y: Integer;
  346.   State: TDragState; var Accept: Boolean);
  347. begin
  348.   accept := (Source is TFileListBox) or (Source is TListBox) or (Source is TCDBMemo);
  349. end;
  350.  
  351. procedure TForm1.CDBImage1DragOver(Sender, Source: TObject; X, Y: Integer;
  352.   State: TDragState; var Accept: Boolean);
  353. begin
  354.   accept := (Source=Image1) or (Source is TCDBImage) or
  355.      (Source is TFileListBox) or (Source is TListBox);
  356. end;
  357.  
  358. { Refreshing a CDBImage so it will be compressed (assuming previously uncompressed) }
  359. procedure TForm1.CDBImage1MouseDown(Sender: TObject; Button: TMouseButton;
  360.   Shift: TShiftState; X, Y: Integer);
  361. begin
  362.   if Button=mbRight then { ok, refresh our field }
  363.   begin
  364.      CDBImage1.CopyToClipBoard;
  365.      CDBImage1.PasteFromClipBoard;
  366.      Table1.post;
  367.   end;
  368. end;
  369.  
  370. procedure TForm1.CDBMemo1MouseDown(Sender: TObject; Button: TMouseButton;
  371.   Shift: TShiftState; X, Y: Integer);
  372. begin
  373.   if Button=mbRight then { ok, refresh our field }
  374.   begin
  375.      CDBMemo1.Lines[0]:=CDBMemo1.Lines[0]; { setting .Modified doesn't do it... }
  376.      Table1.post;
  377.   end;
  378.  
  379. end;
  380.  
  381. procedure TForm1.CMethodClick(Sender: TObject);
  382. begin
  383.   CDBIMage1.CompressionMethod := getCompressionMethod;
  384.   CDBMemo1.CompressionMethod := getCompressionMethod;
  385. end;
  386.  
  387. procedure TForm1.FormCreate(Sender: TObject);
  388. begin
  389.  Application.HelpFile:='COMPRESS.HLP';
  390.  fileList := TStringList.create; { keeps track of our archive files for display etc. }
  391.  SendMessage(ListBox1.handle,LB_SetHorizontalExtent,300,LongInt(0));
  392.  saveCompressionMethod := -1; { see Listbox1.click }
  393.  showfiles; { show files in archive (if any)... }
  394.  try
  395. {$IFDEF WINDOWS}
  396.    DL.Directory := '\DELPHI\IMAGES\BACKGRND';
  397. {$ENDIF}
  398. {$IFDEF WIN32}
  399.  {$IFDEF VER90} { Delphi 2, too bad about C++ Builder... }
  400.    DL.Directory := '\Program Files\Borland\Delphi 2.0\IMAGES\BACKGRND';
  401.  {$ELSE}
  402.    DL.Directory := '\Program Files\Borland\Delphi 3.0\IMAGES\BACKGRND';
  403.  {$ENDIF}   { Delphi 3 is VER100 }
  404.  {$ENDIF} { win32 }
  405.  except on EInOutError do ; { nowt, let it default }
  406.  end;
  407.  
  408.  try Table1.Active := True;
  409.      DataSource1.Edit;
  410.  except
  411.   on EDBEngineError do
  412.      showmessage('The BLOB compression portion of this demonstration'+#13+
  413.                  'requires that the DBDEMOS alias be set up and pointing'+#13+
  414.                  'to the BIOLIFE.DB table in \DELPHI\DEMOS\DATA.'+#13+#13+
  415.                  '-- as this is not currently the case, the BLOB demonstration'+#13+
  416.                  'is disabled.');
  417.   on EUnrecognizedCompressionMethod do
  418.      showmessage('Your BIOLIFE database appears to have been compressed with'+#13+
  419.                  'a custom compression method which cannot be recognised.'+#13+
  420.                  'Please revert to an uncompressed backup of BIOLIFE.*');
  421.  end; {try }
  422.  
  423.  if not Table1.Active then { something went wrong... }
  424.  begin
  425.      CDBImage1.visible:=False;
  426.      CDBMemo1.visible:=False;
  427.      DBNavigator1.visible:=False;
  428.      Memo1.visible:=False;
  429.      Memo2.visible := True;
  430.  end;
  431.  CMethodClick(self);  { get default compression for our database controls }
  432.  
  433. end;
  434.  
  435. function TForm1.GetDir: string; { called below and in GetDummyFileName }
  436. begin
  437.   Result := DL.Directory;
  438.   if Copy(Result,Length(Result),1)<>'\' then { not already \'d? }
  439.     Result := Result+'\';
  440. end;
  441.  
  442. procedure TForm1.archivefileChange(Sender: TObject);
  443. begin
  444.   showfiles;
  445. end;
  446.  
  447. function TForm1.getCompressionMethod: TCompressionMethod;
  448. begin
  449.    result := coNone; { default }
  450.    case CMethod.ItemIndex of
  451.      1: result := coRLE;
  452.      2: result := coLZH;
  453.      3: result := coLZH5;
  454.    end;
  455. end;
  456.  
  457. procedure TForm1.DLDragOver(Sender, Source: TObject; X, Y: Integer;
  458.   State: TDragState; var Accept: Boolean);
  459. begin
  460.   accept := True;
  461.   if ((Sender is TDirectoryListBox) and (Source is TFileListBox)) or
  462.      (Source=Trashcan) then
  463.         accept := False; { fair enough? }
  464. end;
  465.  
  466. procedure TForm1.archivefileDragOver(Sender, Source: TObject; X,
  467.   Y: Integer; State: TDragState; var Accept: Boolean);
  468. begin
  469.   accept := True; { but... }
  470.   if ((Source is TGroupBox) and not (Sender is TGroupBox)) or
  471.          (((Sender is TEdit)or (Sender is TGroupBox)) and (Source is TListBox)) or { not from our OWN list }
  472.            (Source=Trashcan) then
  473.      accept := False;
  474. end;
  475.  
  476. { Used to create 'work' filenames for saving images and memos
  477.   from the database into our archive or to disk... }
  478. function TForm1.GetDummyFilename(generatefrom: string; ext: string): string;
  479. {$IFDEF WINDOWS} { patch in v3.01 }
  480.   var spos: smallint;
  481. {$ENDIF}
  482. begin
  483.   if (generatefrom='Image') or (generateFrom='') then
  484.      generatefrom:='image'
  485.   else
  486.   begin
  487. {$IFDEF WINDOWS}
  488.      generatefrom := copy(generatefrom,1,8); { max 8 }
  489.      spos:=pos(' ',generateFrom);
  490.      while spos >0 do { eliminate spaces }
  491.      begin
  492.         delete(generatefrom,spos,1);
  493.        spos:=pos(' ',generateFrom);
  494.      end;
  495. {$ENDIF}     
  496.   end;
  497.   result := Getdir+generatefrom+'.'+ext;
  498. end;
  499.  
  500. function Confirmfilename(filename: String; archiving: Boolean): Boolean;
  501. begin
  502.   Result := True; { default for archiving }
  503.   if (not Archiving) and
  504.      (MessageDlg('Save to '+filename+'?', mtConfirmation,[mbYes, mbNo], 0)<>id_Yes) then
  505.      Result := False;
  506. end;
  507.  
  508. { The handler for dropping things on the file list or archive list }
  509. procedure TForm1.handleDropField(Source: TObject; archivetoo: Boolean);
  510. var filename: String;
  511. begin
  512.   filename := ''; { in case it is NOT one of those below... }
  513.   if Source is TCDBMemo then
  514.   begin
  515.      filename := GetDummyFilename(Fishname.Text,'TXT');
  516.      if not confirmFilename(filename,archivetoo) then exit;
  517.      if ArchiveToo then { V3.0 -- save directly into archive -- no temp file }
  518.      begin
  519.        SaveDirectToArchive((Source as TCDBMemo).Field,filename);
  520.        exit;
  521.      end else
  522.        CDBMemo1.Lines.SaveToFile(filename); { save to directory }
  523.   end else if Source is TCDBImage then
  524.   begin
  525.      filename := GetDummyFilename(Fishname.Text,'BMP');
  526.      if not confirmFilename(filename,Archivetoo) then exit;
  527.      if ArchiveToo then { V3.0 -- save directly into archive -- no temp file }
  528.      begin
  529.        SaveDirectToArchive((Source as TCDBImage).Field,filename);
  530.        exit;
  531.      end else
  532.         CDBImage1.Picture.Bitmap.SaveToFile(filename); { save to directory }
  533.   end
  534.   else
  535.    if Source = Image1 then
  536.   begin
  537.      filename := GetDummyFilename('Image','BMP');
  538.      if not confirmFilename(filename,Archivetoo) then exit;
  539.      Image1.Picture.Bitmap.SaveToFile(filename);
  540.   end;
  541.   if (filename<>'') and (ArchiveToo) then
  542.       CompressOneFile(filename);
  543. end;
  544.  
  545. { new in V3.0, this routine APPENDS a blob to the archive, after first making
  546.   sure something of the same name is not already there. While this is fast,
  547.   in a working situation it would be tidier with a DeleteFiles call to remove
  548.   any prior copy of the blob first...
  549. }
  550. procedure TForm1.SaveDirectToArchive(Source: TField; filename: string);
  551. var bs: TCBlobstream; { for compressing into the archive: may need to auto-EXPAND first, hence TCBlobstream... }
  552. begin
  553.   filename :=ExtractFileName(filename);
  554.   if FileList.Indexof(filename) >=0 then
  555.   begin
  556.     showmessage(filename+' is already in the archive -- please delete it first');
  557.     exit; { to automate the deletion, we could just use the Compress1.DeleteFiles method }
  558.   end;
  559.   bs := TCBlobstream.Create(Source as TCBlobField,bmRead); { we're going to read the (expanded) field contents) }
  560.   try
  561.      if Source is TCGraphicField then { sorry about this, but we have to skip a graphic header which Delphi stores }
  562.         bs.seek(8,soFromBeginning);  { in blob bitmaps, but which DON'T belong in BMP files -- this very hardwired
  563.                                      code assumes it is there, and skips it }
  564.  
  565.      Screen.cursor := crHourGlass;
  566.      disableDragMode;
  567.      Compress1.CompressStreamToArchive(ArchiveFile.Text,bs, { and append/compress them to the archive... }
  568.                                       filename,getCompressionMethod);
  569.   finally
  570.      enableDragMode;
  571.      Screen.cursor := crDefault;
  572.      bs.free;
  573.   end;
  574.   showinfo;
  575.   showfiles;
  576. end;
  577.  
  578. procedure TForm1.archivefileDragDrop(Sender, Source: TObject; X,
  579.   Y: Integer);
  580. begin
  581.   if Source is TFileListBox then
  582.      CompressFiles
  583.   else
  584.     HandleDropField(Source, True); { save to temp file AND archive... }
  585. end;
  586.  
  587. procedure TForm1.DLDragDrop(Sender, Source: TObject; X, Y: Integer);
  588. begin
  589.   if Source=Sender then exit; { seems reasonable, and IS necessary }
  590.   if Source is TListBox then
  591.     ExpandDelete(cmExpand,False) { selected archive files }
  592.   else if Source=ArchiveGroup then
  593.      ExpandDelete(cmExpand,True) { all archived files }
  594.   else
  595.     HandleDropField(Source, False); { save field to a file }
  596.   FL.Update; { get up to date... }
  597. end;
  598. procedure TForm1.TrashcanDragDrop(Sender, Source: TObject; X, Y: Integer);
  599. var count: Integer;
  600.     tempBitmap: TBitMap; { just to get an empty one }
  601. begin
  602.   if Source is TListBox then
  603.     ExpandDelete(cmDelete,False)
  604.   else if Source=ArchiveGroup then
  605.      ExpandDelete(cmDelete,True) { all files }
  606.      { and strictly speaking, should now delete the archive if it is
  607.        empty, but I'll leave that as an exercise... }
  608.   else if Source is TFileListBox then { delete some or all... }
  609.   begin
  610.      for count:=0 to FL.Items.count-1 do
  611.         if FL.selected[count] and
  612.            (MessageDlg('Delete '+GetDir+FL.Items[count],mtConfirmation,[mbYes,mbNo],0)=id_Yes) then
  613.            SysUtils.DeleteFile(GetDir+FL.Items[count]);
  614.      FL.Update;
  615.   end
  616.   else if (Source is TCDBMemo) and
  617.               (MessageDlg('Cut to clipboard?',mtConfirmation,[mbYes,mbNo],0)=id_Yes) then
  618.   begin
  619.      CDBMemo1.SelectAll;
  620.      CDBMemo1.cutToClipboard { safer than .clear, for demo purposes }
  621.   end
  622.   else if (Source is TCDBImage) and
  623.             (MessageDlg('Cut to clipboard?',mtConfirmation,[mbYes,mbNo],0)=id_Yes) then
  624.       CDBImage1.cutToClipboard { not quite a delete, but just for example... }
  625.   else if Source=Image1 then
  626.   begin
  627.      tempBitMap := TBitMap.Create;
  628.      try
  629.         Image1.Picture.Bitmap.Assign(tempBitMap);
  630.         Image1.visible := False;
  631.         Memo1.visible := True
  632.      finally
  633.         tempBitMap.free;
  634.      end;
  635.   end;
  636.  
  637.  
  638. end;
  639.  
  640. procedure TForm1.FormDestroy(Sender: TObject);
  641. begin
  642.   Compress1.FreeFileList(FileList); { free list and any file information objects in it }
  643.   FileList.free;
  644. end;
  645.  
  646.  
  647. procedure TForm1.ListBox1Click(Sender: TObject);
  648. var cfinfo: TCompressedFileInfo;
  649.     compmethod, percentageval: Integer;
  650. begin
  651.   if listBox1.ItemIndex >=0 then
  652.   begin
  653.      CMethod.Color := ShowFileInfoColor; { make it clear we are showing off a bit... }
  654.      Percentage.Color := ShowFileInfoColor;
  655.      Time.Color := ShowFileInfoColor;
  656.      TimeLabel.Caption := 'Full Size:';
  657.  
  658.      cfinfo:=TCompressedFileinfo(FileList.objects[listBox1.ItemIndex]); { how to get at the other stuff... }
  659.      if cfinfo.Fullsize>0 then
  660.      begin
  661.        if cfinfo.Fullsize>100000 then { makes safe for files >20Mb actually }
  662.          Percentageval := cfinfo.CompressedSize div (cfinfo.Fullsize div 100)
  663.        else
  664.          Percentageval := 100*cfinfo.CompressedSize div cfinfo.Fullsize;
  665.        Percentage.caption:=IntToStr(100-percentageval)+'%'
  666.      end else
  667.        Percentage.caption:='(empty)';
  668.      if cfinfo.locked then
  669.         Percentage.caption := Percentage.caption + ' (locked)';
  670.      Time.caption:= IntToStr((512+cfinfo.Fullsize) div 1024)+' Kb';
  671.      if saveCompressionMethod <0 then
  672.         savecompressionMethod :=cMethod.ItemIndex;
  673.      compMethod :=Integer(cfinfo.CompressedMode);
  674.      if compMethod = 4 then
  675.          compMethod := 3; { force LZH5 to show up as the third box }
  676.      cMethod.ItemIndex :=compMethod;
  677.   end;
  678. end;
  679.  
  680. procedure TForm1.ResetFileInfo;
  681. begin
  682.   if saveCompressionMethod <0 then exit;
  683.   cMethod.ItemIndex:=savecompressionMethod;
  684.   saveCompressionMethod := -1;
  685.   CMethod.Color := clBtnFace;
  686.   Percentage.Color := clWindow;
  687.   Time.Color := clWindow;
  688.   TimeLabel.Caption := 'Time:';
  689.   showInfo; { get the right stuff too... }
  690.   Time.Caption:=''; { but this is meaningless at this point... }
  691. end;
  692.  
  693.  
  694. procedure TForm1.Table1AfterPost(DataSet: TDataset);
  695. begin
  696.   Showinfo;
  697. end;
  698.  
  699. procedure TForm1.Button1Click(Sender: TObject);
  700. begin
  701.   ShowMessage('Drag && Drop at will: compression/expansion'+#13+
  702.   'is automatic.'+#13+#13+
  703.   'Uses TCompress, TCDBMemo and TCDBImage.'+#13+#13+
  704.   'Component Registration and License: $NZ90 (about $US65)'+#13+
  705.   'See registration form in Help or:'+#13+
  706.   'Fax +64-3-384-5138   Email: software@spis.co.nz');
  707. end;
  708.  
  709. procedure TForm1.FLClick(Sender: TObject);
  710. begin
  711.   ResetFileInfo;
  712. end;
  713.  
  714. { Example of OnCheckFile user interface handling routine
  715.   Note that the V2.5 TargetPath property frequently obviates the need
  716.   for any Expand handler, but we've kept it anyway for your
  717.   info. Also, you could Set the MakeDirectories property if
  718.   the target path's should be created if required.
  719. }
  720. procedure TForm1.Compress1CheckFile(var filepath: String;
  721.   mode: TCProcessMode);
  722. var modestr: String;
  723.   dlg: Integer;
  724. begin
  725.   case mode of
  726.      cmExpand: begin
  727.                  modestr := 'Expand';
  728.                  filepath:=Getdir+extractfilename(filepath); { go where we should }
  729.                end;
  730.      cmCompress: begin
  731.                     modestr := 'Compress';
  732.                     filepath:={Getdir+}extractfilename(filepath); { use GetDir if you want full path... }
  733.                  end;
  734.      cmDelete: modestr := 'Delete';
  735.   end;
  736.   showInfo;
  737.   Screen.cursor := crDefault; { in case this is second call in a sequence }
  738.   dlg := MessageDlg(modestr+' '+filepath+'?', mtConfirmation,[mbYes, mbNo, mbCancel], 0);
  739.   case dlg of
  740.      id_No: filepath :=CompressSkipFlag; { flag 'not this one'}
  741.      id_Cancel: filepath :=CompressNoMoreFlag; { flag 'no more!' }
  742.      id_Yes: Screen.Cursor := crHourGlass; { for operation itself }
  743.   end;
  744. end;
  745.  
  746.  
  747. procedure TForm1.Panel1Click(Sender: TObject);
  748. begin
  749. ResetFileInfo;
  750. end;
  751.  
  752. procedure TForm1.FormClick(Sender: TObject);
  753. begin
  754. ResetFileInfo;
  755. end;
  756.  
  757. procedure TForm1.GroupBox1Click(Sender: TObject);
  758. begin
  759. ResetFileInfo;
  760. end;
  761.  
  762. procedure TForm1.TrashcanDragOver(Sender, Source: TObject; X, Y: Integer;
  763.   State: TDragState; var Accept: Boolean);
  764. begin
  765.   accept := True;
  766. end;
  767.  
  768. procedure TForm1.Button2Click(Sender: TObject);
  769. begin
  770. Application.HelpJump('1050');
  771. end;
  772.  
  773. procedure TForm1.Button3Click(Sender: TObject);
  774. begin
  775. Application.HelpJump('1030');
  776. end;
  777.  
  778. { V3.03 -- disable dragging temporarily while compression
  779.   is in progress, because otherwise it is *possible* (tho unlikely)
  780.   to request a second compression before the first has finished,
  781.   i.e. code is no longer re-entrant via the user interface.
  782. }
  783. procedure TForm1.disableDragMode;
  784. begin
  785.   Fl.dragMode := dmManual;
  786.   CDBMemo1.dragMode := dmManual;
  787.   CDBImage1.dragMode := dmManual;
  788.   ArchiveGroup.dragMode := dmManual;
  789.   ListBox1.dragMode := dmManual;
  790. end;
  791.  
  792. procedure TForm1.enableDragMode;
  793. begin
  794.   Fl.dragMode := dmAutomatic;
  795.   CDBMemo1.dragMode := dmAutomatic;
  796.   CDBImage1.dragMode := dmAutomatic;
  797.   ArchiveGroup.dragMode := dmAutomatic;
  798.   ListBox1.dragMode := dmAutomatic;
  799. end;
  800.  
  801.  
  802. end.
  803.